#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import codecs

import os

import xlrd

from data_prepare import loadData

__author__ = 'jxliu.nlper@gmail.com'
import sys

print(sys.path)

sys.path.append("c:\\users\\newuser\\appdata\\local\\programs\\python\\python36-32\\lib\\site-packages")
"""
    标记文件
"""
import yaml
import pickle
import tensorflow as tf
from load_data import load_vocs, init_data
from model import SequenceLabelingModel
from deal import writetxt, reDealText
import nltk

# 加载配置文件
with open('./config.yml', 'rb') as file_config:
    # config = yaml as yaml.load(file_config)
    config = yaml.load(file_config)

feature_names = config['model_params']['feature_names']

# 初始化embedding shape, dropouts, 预训练的embedding也在这里初始化)
feature_weight_shape_dict, feature_weight_dropout_dict, \
feature_init_weight_dict = dict(), dict(), dict()
for feature_name in feature_names:
    feature_weight_shape_dict[feature_name] = \
        config['model_params']['embed_params'][feature_name]['shape']
    feature_weight_dropout_dict[feature_name] = \
        config['model_params']['embed_params'][feature_name]['dropout_rate']
    path_pre_train = config['model_params']['embed_params'][feature_name]['path']
    if path_pre_train:
        with open(path_pre_train, 'rb') as file_r:
            feature_init_weight_dict[feature_name] = pickle.load(file_r)

# 加载vocs
path_vocs = []
for feature_name in feature_names:
    path_vocs.append(config['data_params']['voc_params'][feature_name]['path'])
path_vocs.append(config['data_params']['voc_params']['label']['path'])
vocs = load_vocs(path_vocs)
print(vocs[-1])
print(len(vocs))
# 加载模型
model = SequenceLabelingModel(
    sequence_length=config['model_params']['sequence_length'],
    nb_classes=config['model_params']['nb_classes'],
    nb_hidden=config['model_params']['bilstm_params']['num_units'],
    feature_weight_shape_dict=feature_weight_shape_dict,
    feature_init_weight_dict=feature_init_weight_dict,
    feature_weight_dropout_dict=feature_weight_dropout_dict,
    dropout_rate=config['model_params']['dropout_rate'],
    nb_epoch=config['model_params']['nb_epoch'], feature_names=feature_names,
    batch_size=config['model_params']['batch_size'],
    train_max_patience=config['model_params']['max_patience'],
    use_crf=config['model_params']['use_crf'],
    l2_rate=config['model_params']['l2_rate'],
    rnn_unit=config['model_params']['rnn_unit'],
    learning_rate=config['model_params']['learning_rate'],
    path_model=config['model_params']['path_model'])


def predict(string):
    chioceAction = []
    chioceTarget = []
    chioceData = []
    lab = writetxt(string)
    # 加载数据
    if len(lab[0]) == 0:
        return 'ok;None'
    sep_str = config['data_params']['sep']
    assert sep_str in ['table', 'space']
    sep = '\t' if sep_str == 'table' else ' '
    data_dict = init_data(
        path=config['data_params']['path_test'], feature_names=feature_names, sep=sep,
        vocs=vocs, max_len=config['model_params']['sequence_length'], model='test')

    saver = tf.train.Saver()
    saver.restore(model.sess, config['model_params']['path_model'])

    seq = model.predict(data_dict)
    print(seq)
    for i in range(len(seq)):
        delOne = ''
        if (6 in seq[i] or 11 in seq[i] or 7 in seq[i] or 10 in seq[i]):
            tem = ""
            for j in range(len(seq[i])):
                if seq[i][j] == 6  or seq[i][j] == 7:
                    tem += lab[i][j]
                if seq[i][j]==10:
                    chioceAction.append(lab[i][j])
            if len(tem) >0:
                chioceAction.append(tem)
    ch = '***'.join(chioceAction)
    finalAction = '' + ch
    if finalAction == '':
        finalAction = '0'
    for i in range(len(seq)):
        if (4 in seq[i] or 3 in seq[i] or 5 in seq[i] or 13 in seq[i]):
            tem = ""
            for j in range(len(seq[i])):
                if seq[i][j] == 4 or seq[i][j] == 3 or seq[i][j] == 5:
                    tem += lab[i][j]
                if seq[i][j]==13:
                    chioceTarget.append(lab[i][j])
            if len(tem)>0:
                chioceTarget.append(tem)
    ch = '***'.join(chioceTarget)
    finalTarget = '' + ch
    if finalTarget == '':
        finalTarget = '0'
    for i in range(len(seq)):
        if (8 in seq[i] or 2 in seq[i] or 9 in seq[i] or 12 in seq[i]):
            tem = ""
            for j in range(len(seq[i])):
                if seq[i][j] == 8 or seq[i][j] == 2 or seq[i][j] == 9:
                    tem += lab[i][j]
                if seq[i][j]==12:
                    chioceData.append(lab[i][j])
            if len(tem)>0:
                chioceData.append(tem)
    ch = '***'.join(chioceData)
    finalData = '' + ch
    if finalData == '':
        finalData = '0'
    return finalAction, finalTarget, finalData


# finalA = []
# finalT = []
# txtpath = 'C:/Users/NewUser/Desktop/test.txt'
# fp = open(txtpath)
# fw = codecs.open('data/test_result.txt', 'w','utf-8')
# #要去标点
# for linea in fp.readlines():
#     linea = linea.strip()
#     print(linea)
#     resultp, resultt = predict(linea)
#     line = ''.join([str(resultp) + '\t' + str(resultt)]) + '\n'
#     fw.writelines(line)
#     finalA.append(resultp)
#     finalT.append(resultt)
# # print(finalA)
# # print(finalT)
# fw.close()

resultp, resultt, da = predict('点击翻页功能栏中的填写框填写1')
print(resultp, resultt, da)
